<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container"></div>
    <!--数据列表-->
    <div class="data-table">
      <el-table :data="responseData.tableList"
                border style="width: 100%;"
                v-loading="pageData.loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :header-cell-style="{ background: '#545C64', color: '#ffffff' }"
                @selection-change="handleTableSelection">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column fixed="left" prop="articleId" label="ID" width="320" align="center"></el-table-column>
        <el-table-column prop="articleTitle" label="标题" width="320" align="center"></el-table-column>
        <el-table-column prop="articleType" label="类型" width="200" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.articleType === 1"><el-tag type="warning">文章</el-tag></span>
            <span v-if="scope.row.articleType === 2"><el-tag type="success">政采播报</el-tag></span>
            <span v-if="scope.row.articleType === 3"><el-tag type="primary">视频</el-tag></span>
            <span v-if="scope.row.articleType === 4"><el-tag type="primary">视频</el-tag></span>
            <span v-if="scope.row.articleType === 5"><el-tag type="danger">红茶坊</el-tag></span>
            <span v-if="scope.row.articleType === 6"><el-tag type="primary">视频</el-tag></span>
            <span v-if="scope.row.articleType === 7"><el-tag type="primary">视频</el-tag></span>
            <span v-if="scope.row.articleType === 8"><el-tag type="primary">视频</el-tag></span>
            <span v-if="scope.row.articleType === 9"><el-tag type="info">音频</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="articleImage" label="展示图" width="120" align="center">
          <template slot-scope="scope">
              <span v-if="scope.row.articleDisplayImage != null && scope.row.articleDisplayImage !== ''">
                <el-button size="small" width="120" type="success" @click="handleCheckInfo(scope)">预览</el-button>
              </span>
            <span v-else >无</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="240" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="handleOpenEditPanel(scope)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页器-->
    <div style="margin-top: 20px">
      <div class="block" style="width: 100%; text-align: center">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="form.pageNum"
          :page-size="form.pageSize"
          :total="form.total"
          :page-sizes="[12, 50, 100, 200]"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange">
        </el-pagination>
      </div>
    </div>
    <!--图片预览隐藏域-->
    <el-dialog title="图像预览" :visible.sync="pageData.checkPanelStatus" size="tiny" width="480px">
      <el-card :body-style="{ padding: '0px' }">
        <div>
          <div>
            <img :src="pageData.checkPanelInfo" width="450px" height="450px">
          </div>
        </div>
      </el-card>
    </el-dialog>
    <!--修改推荐隐藏域-->
    <el-dialog title="推荐预设" :visible.sync="pageData.editPanelStatus" size="tiny"
               :before-close="handleCloseEditPanel">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="推荐文章">
          <el-select v-model="form.articleId"
                     filterable remote reserve-keyword
                     placeholder="请选择推荐文章"
                     :remote-method="handleRemoteData">
            <el-option
              v-for="item in responseData.remoteSearchList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              <span style="float: left">{{ item.label }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.type }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="展示图">
          <el-upload
            class="avatar-uploader"
            action="/api/cms/common/uploadFile"
            :show-file-list="false"
            :on-success="handleArticleDisplayImageUploadSuccess">
            <img v-if="form.articleDisplayImage" :src="form.articleDisplayImage" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <el-button type="primary" size="small" @click="handleSaveButtonClick">确定修改</el-button>
    </el-dialog>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsRecommendList,loadCmsArticleDataList,updateCmsRecommendData } from "@/api/config";
  export default {
    name: "recommend",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
      return {
        pageData:{
          loading: false,
          editPanelStatus: false,
          checkPanelStatus: false,
          checkPanelInfo: ""
        },// 页面配置数据
        form:{
          pageNum: 1,
          pageSize: 12,
          total: 0,
          articleId: "",
          articleIndex: 0,
          articleDisplayImage: "",
        },// 页面表单
        formRules: {},// 表单验证
        responseData: {
          tableList: [],
          remoteSearchList: []
        },// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods:{
      // 事件函数
      handlePageInit(){
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsRecommendList();// 读取路由角色列表
      },// 页面初始化事件
      handleCurrentChange(val){
        this.form.pageNum = val;// 更换页码
        this.handlePageInit();// 进行数据加载
      },// 页码切换事件
      handleSizeChange(val){
        this.form.pageSize = val;// 更换页面大小
        this.handlePageInit();// 进行数据加载
      },// 页面size切换事件
      handleTableSelection(val){
        let list = [];// 初始化多选数组
        for (let i=0; i<val.length; i++){
          list.push(val[i].articleId);
        }// 遍历多选的列表，将ID拼入数组中
        this.form.selection = list;// 进行多选值的切换
      },// 表格多选事件
      handleCheckInfo(scope){
        this.pageData.checkPanelStatus = true;
        this.pageData.checkPanelInfo = scope.row.articleDisplayImage;
      },// 预览图像(查看表格详情)
      handleOpenEditPanel(scope){
        // 初始化参数
        this.form.articleId = scope.row.articleId;
        this.form.articleIndex = scope.$index;
        this.form.articleDisplayImage = scope.row.articleDisplayImage;
        // 打开面板
        this.pageData.editPanelStatus = true;
      },// 打开修改面板
      handleCloseEditPanel(){
        this.$confirm('确认关闭？').then(response => {
          // 初始化参数
          this.form.articleId = "";
          this.form.articleIndex = 0;
          this.form.articleDisplayImage = "";
          // 打开面板
          this.pageData.editPanelStatus = false;
        }).catch(err => {
          console.log(err);
        });
      },// 关闭修改面板
      handleRemoteData(query){
        if (query !== ""){
          let param = {
            searchTitle: query,
          };
          this.loadCmsArticleDataList(param);
        }
      },// 远程搜索可用文章
      handleArticleDisplayImageUploadSuccess(response){
        this.form.articleDisplayImage = response.data;
      },// 图片上传成功
      handleSaveButtonClick(){
        this.$confirm('该操作将会保存当前数据, 请核对后继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.updateCmsRecommendData();
        }).catch(close => {
          console.log(close);
          this.$message.info("已取消操作");
        });
      },// 保存按钮点击事件
      // 接口
      loadCmsRecommendList(){
        loadCmsRecommendList(this.form).then(response => {
          if (response.code === "200"){
            this.pageData.loading = false;
            this.responseData.tableList = response.data.list;
            this.form.total = response.data.total;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取列表数据
      loadCmsArticleDataList(param){
        loadCmsArticleDataList(param).then(response => {
          if (response.code === "200"){
            this.responseData.remoteSearchList = response.data;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 搜索可用文章
      updateCmsRecommendData(){
        updateCmsRecommendData(this.form).then(response => {
          if (response.code === "200"){
            this.$message.success("更新成功");
            // 初始化参数
            this.form.articleId = "";
            this.form.articleIndex = 0;
            this.form.articleDisplayImage = "";
            // 打开面板
            this.pageData.editPanelStatus = false;
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      }// 更新推荐内容
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function () {
      let _this = this;
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
